define(function(require, exports, module) {
    
    var rk = require('rk');
    
    require('../topic/topicOperationCtrl');
    require('./blogUpdateCtrl');
    $.widget('rk.blogContentCtrl', $.rk.oaContentCtrl, {
        _create: function() {
            var me = this;
            me._listeners();
        },
        _listeners: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            //elem.on("click", "[act='blog_tag_edit']", function(){
            //    var button = $(this);
            //    button.hide();
            //    elem.find("[act='blog_tag_save']").show();
            //    //elem.find("[act='blog_tag_search_div']").topicOperationCtrl({type:"update"});
            //});
            //elem.on("click", "[act='blog_tag_save']", function(){
            //    var button = $(this);
            //    button.hide();
            //    elem.find("[act='blog_tag_edit']").show();
            //    //elem.find("[act='blog_tag_search_div']").topicOperationCtrl({type:"search",systemId:505,systemItemId:opt.bid});
            //    //elem.find("[act='blog_tag_search_div']").topicOperationCtrl({type:"success"});
            //});
            elem.on("click", "[act='blog_update_dialog_button']", function() {
                var button = $(this);
                rk.dialog({
                    title: rk.i18n('BLOG_MODIFY'),
                    width:"930",
                    success: function(contentElem) {
                        contentElem.blogUpdateCtrl({
                            bid: opt.bid
                        }); //初始化弹框内容的Ctrl
                    },
                    footer: ['cancel', {
                        act: 'ok',
                        visible: true,
                        text: rk.i18n('SAVE')
                    }]
                });
            });
            elem.on("click", "[act='blog_stars']", function() {
                var $this = $(this);
                var $li = $this.parent();
                if (!$li.siblings('li.current-rating').hasClass('current-rating0')) return;
                var param = {
                    blogId: opt.bid,
                    starRate: $this.attr('data-value') - 0
                };
                rk.get('/json/oa_blog/star.action', param, function(reData) {
                    if (reData.status == 0) {
                        //var starClassNO = Number($this.attr('title')) * 2;
                        var starClassNO = reData.star - 0;
                        $li.siblings('li.current-rating').removeClass('current-rating0').addClass('current-rating' + starClassNO / 5);
                        //$li.siblings('li.current-rating').nextAll().remove();
                        elem.find("[act='blog_stars']").remove();
                    }
                });
            });
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            //var require("oa/tmpl/blog/tpl_blog_content");
            me.loaddata();
        },
        loaddata: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            //elem.html(temp);
            rk.post("/json/oa_blog/blog-detail.action", {
                blogId: opt.bid
            }, function(json) {
                var temp = require("oa/tmpl/blog/tpl_blog_content.tpl");
                if (json.status === 0) {
                    if (json.blog) {
                        var html = rk.templateText(temp, json);
                        elem.html(html);
                        elem.find(".blog_content_text").beScroll({
                            axis:'yx'
                        });
                        elem.find("[act='blog_tag_search_div']").topicOperationCtrl({
                            type: "search",
                            systemId: 505,
                            systemItemId: opt.bid,
                            successButton: elem.find("[act='blog_tag_save']"),
                            updateButton: elem.find("[act='blog_tag_edit']")
                        });
                        elem.find('[act="blog_comments"]').comment({
                            systemId: 505,
                            systemItemId: opt.bid,
                            previewList: [],
                            allusers: [],
                            listPosition: 'top'
                        });
                    } else {
                        var temp = require("oa/tmpl/blog/tpl_error_blog.tpl");
                        var html = rk.templateText(temp, {
                            text: rk.i18n('BLOG_HAS_DELETED')
                        });
                        elem.html(html);
                        elem.show();
                    }
                } else {
                    //rk.noticeError("系统错误");
                    var temp = require("oa/tmpl/blog/tpl_error_blog.tpl");
                    var html = rk.templateText(temp, {
                        text: rk.i18n('COLLEAGUE_CANT_VISIT_ERROR')
                    });
                    elem.html(html);
                    elem.show();
                }
            });
        }
    });
    return {
        init: function(rkContent, id) {
            rkContent.blogContentCtrl({
                bid: id
            });
        }
    }
});
